@file:OptIn(ExperimentalMaterial3Api::class)

package com.feeyo.groundservice.composewidget.view.progress

import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.ProgressIndicatorDefaults
import androidx.compose.material3.VerticalDivider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.times
import com.feeyo.groundservice.composewidget.R
import com.feeyo.groundservice.composewidget.util.dimenToDp
import com.feeyo.groundservice.composewidget.util.dpToPx
import com.feeyo.groundservice.composewidget.util.toComposeColor

/**
 *作者：QLH on 2024-10-21
 *描述：进度条
 */


/**水平进度条
 * @param progress 进度值
 * @param progressWidth 进度条宽度
 * @param progressHeight 进度条高度
 * @param completeColor 进度条完成颜色
 * @param bgColor 进度条背景颜色
 * @param trackIconRes 进度条指示器图标
 * @param trackIconResColor 进度条指示器图标颜色
 * @param strokeCap 进度条端点样式
 * @param gapSize 进度条间隔
 * @param drawStopIndicator 进度条停止器绘制，就是在最后绘制一个啥东西，感觉没啥用
 * **/
@Composable
fun CustomLinearProgressIndicator(
    progress: Float,
    progressWidth: Dp = R.dimen.d100.dimenToDp(),
    progressHeight: Dp = R.dimen.d2.dimenToDp(),
    completeColor: Color = R.color.blue_2E76EE.toComposeColor(),
    bgColor: Color = R.color.gray_99.toComposeColor(),
    trackIconRes: Int? = null,
    trackIconResColor: Color = R.color.green_1FBB7D.toComposeColor(),
    strokeCap: StrokeCap = StrokeCap.Square,
    gapSize: Dp = ProgressIndicatorDefaults.LinearIndicatorTrackGapSize,
    drawStopIndicator: DrawScope.() -> Unit = {}
) {

    Box(
        modifier = Modifier.width(progressWidth),
        contentAlignment = Alignment.CenterStart
    ) {
        val completeWidth = remember { mutableStateOf(0.dp) }
        val offset = animateDpAsState(
            targetValue = completeWidth.value,
            animationSpec = tween(durationMillis = 500),
            label = "offset"
        )

        LinearProgressIndicator(
            progress = { progress },
            modifier = Modifier.fillMaxWidth().height(progressHeight),
            color = completeColor,
            trackColor = bgColor,
            strokeCap = strokeCap,
            gapSize = gapSize,
            drawStopIndicator = drawStopIndicator
        )

        trackIconRes?.let {
            Image(
                painter = painterResource(it),
                contentDescription = "",
                modifier = Modifier.size(R.dimen.d13.dimenToDp())
                    .offset(x = offset.value),
                colorFilter = ColorFilter.tint(trackIconResColor)
            )
        }


        val currentOffset = progress * progressWidth
        //飞机偏移不超过进度条
        val target = if (currentOffset > progressWidth - R.dimen.d13.dimenToDp())
            progressWidth - R.dimen.d13.dimenToDp()
        else
            currentOffset

        LaunchedEffect(key1 = target) {
            completeWidth.value = target
        }
    }
}

/**垂直进度条,没有先现成的像LinearProgressIndicator，只能手写一个
 * @param progress 进度值
 * @param progressWidth 进度条宽度
 * @param progressHeight 进度条高度
 * @param completeColor 进度条完成颜色
 * @param bgColor 进度条背景颜色
 * @param trackIconRes 进度条指示器图标
 * @param trackIconResColor 进度条指示器图标颜色
 *@param capRadius 线条2端cap的样式：圆角
 * **/
@Composable
fun CustomVerticalProgressIndicator(
    progress: Float,
    progressWidth: Dp = R.dimen.d2.dimenToDp(),
    progressHeight: Dp = R.dimen.d100.dimenToDp(),
    completeColor: Color = R.color.blue_2E76EE.toComposeColor(),
    bgColor: Color = R.color.gray_99.toComposeColor(),
    trackIconRes: Int? = null,
    trackIconResColor: Color = R.color.green_1FBB7D.toComposeColor(),
    capRadius:Dp = R.dimen.d0.dimenToDp(),
) {
    Box(
        modifier = Modifier.height(progressHeight),
        contentAlignment = Alignment.BottomCenter
    ) {

        val completeHeight = remember { mutableStateOf(0.dp) }
        val offset = animateDpAsState(
            targetValue = completeHeight.value,
            animationSpec = tween(durationMillis = 500),
            label = "offset"
        )
        //全进度线
        VerticalDivider(
            color = bgColor,
            thickness = progressWidth,
            modifier = Modifier.height(progressHeight)
                .clip(RoundedCornerShape(capRadius))
        )

        //走过的进度线
        VerticalDivider(
            color = completeColor,
            thickness = progressWidth,
            modifier = Modifier.height(offset.value)
                .clip(RoundedCornerShape(capRadius))
        )

        trackIconRes?.let {
            Image(
                painter = painterResource(it),
                contentDescription = "",
                modifier = Modifier.size(R.dimen.d13.dimenToDp())
                    .offset(y = offset.value*(-1))
                    .rotate(-90f),
                colorFilter = ColorFilter.tint(trackIconResColor)
            )
        }


        val currentOffset = progress * progressHeight
        //飞机偏移不超过进度条
        val target = if (currentOffset > progressHeight - R.dimen.d13.dimenToDp())
            progressHeight - R.dimen.d13.dimenToDp()
        else
            currentOffset

        LaunchedEffect(key1 = progress) {
            completeHeight.value = target
        }

    }
}